package com.tbit.uqbike.client.remote.sms;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;


/**
 * @author: LJ:
 */
@Slf4j
public class AliSmsQuBike {
    private static final String domain = "dysmsapi.aliyuncs.com";

    /**
     * 阿里云接入信息
     */
    private static String accessKeyId = "LTAIdfKErZxxWF5u";
    private static String accessKeySecret = "OScVLrYDs6A4ys76IatLrWBqxmHUVt";
    private static String productname = "UQBIKE";//国际、港澳台、国内短信签名
    private static String templateCode = "SMS_172008684";//国际、港澳台短信模板id
    private static String regionId = "cn-hangzhou";
    private static String templateCodeCn = "SMS_172351531";//国内短信模板id

    /**
     * 发送短信
     *
     * @param phone    手机号码
     * @param code     验证码
     * @param areaCode 区号
     */
    public static void sendSms(String phone, String code, String areaCode) {
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
        String currTemplateCode = null;//当前短信模板id
        if (areaCode != null && !"86".equals(areaCode)) {//国际、港澳台短信
            phone = areaCode + phone;
            currTemplateCode = templateCode;
        } else {//国内短信
            currTemplateCode = templateCodeCn;
        }
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain(domain);
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", regionId);
        request.putQueryParameter("PhoneNumbers", phone);//接收短信的手机号
        request.putQueryParameter("SignName", productname);
        request.putQueryParameter("TemplateCode", currTemplateCode);
        request.putQueryParameter("TemplateParam", "{\"code\":\"" + code + "\"}");//验证码json串
        CommonResponse sendSmsResponse = null;
        try {
            sendSmsResponse = client.getCommonResponse(request);
            // System.out.println(sendSmsResponse.getData());
        } catch (Exception e) {
            log.error("连接阿里云短信服务异常", e);
        }
        /**发送短信失败错误打印*/
        if (sendSmsResponse == null) {
            log.error("短信发送失败,phone=" + phone + "code=" + code);
            return;
        }
        //System.out.println("phone:"+phone+"==code:"+code+"====sendSmsResponse===="+JSONObject.toJSONString(sendSmsResponse.getData()));
        Map data = JSONObject.parseObject(sendSmsResponse.getData(), Map.class);
        if (sendSmsResponse.getHttpStatus() != 200 || !"OK".equals(data.get("Code"))) {
            log.error("阿里云短信错误信息=" + data.get("Message"));
        }

    }

    public static void main(String[] args) {
        sendSms("15079017029", "5566", "86");
        //sendSms("2629025508","9999","001");
    }
}
